
* table 1 fits KS and DD to the JD distribution. 

* make JD data
clear all
set obs 6
gen roll1 = _n-1
gen reports = (2*roll1+1)/36
list

* set up a results dataset in a different frame 
frame create results
frame change results
set obs 0
gen theta_hat = .
gen rmse_DD = . 
gen rmse_KS = . 
frame change default

* loop over models and samples. 
gen outcome=roll1 
gen dd_hat=.

	** run the do files to fit dd and ks, and save their predictions
	do "code/fit dd 1 roll.do"
	do "code/fit ks 1 roll.do"
	rename predictions_ks ks_hat
	replace dd_hat=dd_hat*100
	replace reports=reports*100
	
	** save model fit stats
	constraint 3 dd_hat = 1
	cnsreg reports dd_hat, constraint(3) nocons
	local rmse =  e(rmse) * sqrt(e(df_r) / e(N))
	frame change results
	set obs `=_N+1'
	replace rmse_DD = `rmse' if _n==_N
	scalar rmse_DD_scalar = `rmse' 
	frame change default

	constraint 4 ks_hat = 1
	cnsreg reports ks_hat, constraint(4) nocons
	local rmse =  e(rmse) * sqrt(e(df_r) / e(N))
	frame change results
	replace rmse_KS = `rmse' if _n==_N
	scalar rmse_KS_scalar = `rmse' 
	frame change default

list roll1 reports  dd_hat      ks_hat 
frame change results
list

* then making it pretty for latex

frame change default
gsort -roll1 -reports -outcome -dd_hat -ks_hat
set obs 8
replace dd_hat= rmse_DD_scalar if _n==8
replace ks_hat= rmse_KS_scalar if _n==8


drop outcome
rename reports JD
rename dd DD 
rename ks KS
rename roll1 report
replace DD=.05*DD[1] + .04*DD[2]  + .03*DD[3]  + .02*DD[4]  + .01*DD[5]  if _n==7
replace JD=.05*JD[1] + .04*JD[2]  + .03*JD[3]  + .02*JD[4]  + .01*JD[5]  if _n==7
replace KS=.05*KS[1] + .04*KS[2]  + .03*KS[3]  + .02*KS[4]  + .01*KS[5]  if _n==7

format JD DD KS %9.2f

list
tostring report, replace
replace report="Mean Claim" if _n==7
replace report="RMSE" if _n==8

texsave using "output/table1.tex", varlabels hlines(-2) nofix replace frag
